// app.js
App({
  onLaunch() {
    // 小程序启动时执行
    console.log('青藤之恋小程序启动');
    
    // 检查登录状态
    this.checkLoginStatus();
  },
  
  onShow() {
    // 小程序显示时执行
    console.log('青藤之恋小程序显示');
  },
  
  onHide() {
    // 小程序隐藏时执行
    console.log('青藤之恋小程序隐藏');
  },
  
  globalData: {
    userInfo: null,
    token: null,
    baseUrl: 'https://192.168.3.35/api/v1', // 替换为实际API地址
    useMockData: true // 启用模拟数据
  },
  
  // 检查登录状态
  checkLoginStatus() {
    const token = wx.getStorageSync('token');
    const userInfo = wx.getStorageSync('userInfo');
    
    if (token && userInfo) {
      this.globalData.token = token;
      this.globalData.userInfo = userInfo;
    }
  },
  
  // 登录方法
  login(callback) {
    if (this.globalData.useMockData) {
      // 使用模拟数据登录
      const mockUser = {
        id: 'user_current',
        nickname: '测试用户',
        avatar_url: 'https://via.placeholder.com/150x150?text=当前用户',
        age: 25,
        city: '北京',
        gender: 'male'
      };
      
      const mockToken = 'mock_token_123456';
      
      wx.setStorageSync('token', mockToken);
      wx.setStorageSync('userInfo', mockUser);
      
      this.globalData.token = mockToken;
      this.globalData.userInfo = mockUser;
      
      callback && callback(true, mockUser);
      return;
    }
    
    wx.login({
      success: async (res) => {
        if (res.code) {
          try {
            const result = await wx.request({
              url: `${this.globalData.baseUrl}/users/wechat-login`,
              method: 'POST',
              data: { code: res.code }
            });
            
            if (result.data.success) {
              const { token, user } = result.data.data;
              wx.setStorageSync('token', token);
              wx.setStorageSync('userInfo', user);
              
              this.globalData.token = token;
              this.globalData.userInfo = user;
              
              callback && callback(true, user);
            } else {
              callback && callback(false, result.data.message);
            }
          } catch (error) {
            console.error('登录失败:', error);
            callback && callback(false, '登录失败');
          }
        }
      },
      fail: (error) => {
        console.error('微信登录失败:', error);
        callback && callback(false, '微信登录失败');
      }
    });
  },
  
  // 获取用户信息
  getUserProfile() {
    return new Promise((resolve, reject) => {
      wx.getUserProfile({
        desc: '用于完善会员资料',
        success: (res) => {
          resolve(res.userInfo);
        },
        fail: (error) => {
          reject(error);
        }
      });
    });
  },
  
  // 更新用户信息
  async updateUserInfo(userInfo) {
    try {
      const result = await wx.request({
        url: `${this.globalData.baseUrl}/users/me`,
        method: 'PATCH',
        header: { 
          Authorization: `Bearer ${this.globalData.token}` 
        },
        data: {
          nickname: userInfo.nickName,
          avatar_url: userInfo.avatarUrl,
          gender: userInfo.gender,
          city: userInfo.city
        }
      });
      
      return result.data;
    } catch (error) {
      console.error('更新用户信息失败:', error);
      throw error;
    }
  },
  
  // 显示错误提示
  showError(message) {
    wx.showToast({
      title: message,
      icon: 'error',
      duration: 2000
    });
  },
  
  // 显示成功提示
  showSuccess(message) {
    wx.showToast({
      title: message,
      icon: 'success',
      duration: 2000
    });
  }
});
